home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / ctask.zip / TSKBUF.C < prev    next >
C/C++ Source or Header  |  1988-07-01  |  4KB  |  218 lines

  1. /*
  2.    TSKBUF.C - CTask - Buffered Message handling routines.
  3.  
  4.    Public Domain Software written by
  5.       Thomas Wagner
  6.       Patschkauer Weg 31
  7.       D-1000 Berlin 33
  8.       West Germany
  9.  
  10.    NOTE: None of the Buffer routines my be used from inside an interrupt
  11.          handler. The routines are relatively slow, since they use
  12.          the normal word pipe and resource calls. Optimization would be 
  13.          possible by using block moves and internally handling resources.
  14. */
  15.  
  16. #include <stdio.h>
  17.  
  18. #include "tsk.h"
  19. #include "tsklocal.h"
  20.  
  21.  
  22. bufferptr far create_buffer (bufferptr buf, farptr pbuf, word bufsize
  23. #if (TSK_NAMEPAR)
  24.                         ,byteptr name
  25. #endif
  26.                         )
  27. {
  28. #if (TSK_DYNAMIC)
  29.    if (buf == NULL)
  30.       {
  31.       if ((buf = tsk_alloc (sizeof (buffer))) == NULL)
  32.          return NULL;
  33.       buf->flags = F_TEMP;
  34.       }
  35.    else
  36.       buf->flags = 0;
  37.    if (pbuf == NULL)
  38.       {
  39.       if ((pbuf = tsk_alloc (bufsize)) == NULL)
  40.          {
  41.          if (buf->flags & F_TEMP)
  42.             tsk_free (buf);
  43.          return NULL;
  44.          }
  45.       buf->flags |= F_STTEMP;
  46.       }
  47. #endif
  48.  
  49.    create_wpipe (&buf->pip, pbuf, bufsize
  50. #if (TSK_NAMEPAR)
  51.                  ,name
  52. #endif
  53.                  );
  54.    create_resource (&buf->buf_read
  55. #if (TSK_NAMEPAR)
  56.                  ,name
  57. #endif
  58.                  );
  59.    create_resource (&buf->buf_write
  60. #if (TSK_NAMEPAR)
  61.                  ,name
  62. #endif
  63.                  );
  64.    buf->msgcnt = 0;
  65.  
  66. #if (TSK_NAMED)
  67.    tsk_add_name (&buf->name, name, TYP_BUFFER, buf);
  68. #endif
  69.  
  70.    return buf;
  71. }
  72.  
  73.  
  74. void far delete_buffer (bufferptr buf)
  75. {
  76.    delete_wpipe (&buf->pip);
  77.    delete_resource (&buf->buf_read);
  78.    delete_resource (&buf->buf_write);
  79.    buf->msgcnt = 0;
  80.  
  81. #if (TSK_NAMED)
  82.    tsk_del_name (&buf->name);
  83. #endif
  84.  
  85. #if (TSK_DYNAMIC)
  86.    if (buf->flags & F_STTEMP)
  87.       tsk_free (buf->pip.wcontents);
  88.    if (buf->flags & F_TEMP)
  89.       tsk_free (buf);
  90. #endif
  91. }
  92.  
  93.  
  94. int far read_buffer (bufferptr buf, farptr msg, int size, dword timeout)
  95. {
  96.    int i, len, l1, l2;
  97.    word w;
  98.  
  99.    if ((i = request_resource (&buf->buf_read, timeout)) < 0)
  100.       return i;
  101.  
  102.    if ((len = read_wpipe (&buf->pip, timeout)) < 0)
  103.       {
  104.       release_resource (&buf->buf_read);
  105.       return len;
  106.       }
  107.  
  108.    l1 = (len < size) ? len : size;
  109.  
  110.    for (i = 0; i < l1; i++)
  111.       {
  112.       if (!(i & 1))
  113.          w = read_wpipe (&buf->pip, 0L);
  114.       else
  115.          w = w >> 8;
  116.       ((byteptr)msg) [i] = (byte)w;
  117.       }
  118.  
  119.    l2 = (len + 1) >> 1;
  120.    for (i = (l1 + 1) >> 1; i < l2; i++)
  121.       read_wpipe (&buf->pip, 0L);
  122.    if (l1 < size)
  123.       ((byteptr)msg) [l1] = 0;
  124.  
  125.    buf->msgcnt--;
  126.  
  127.    release_resource (&buf->buf_read);
  128.    return len;
  129. }
  130.  
  131.  
  132. int far c_read_buffer (bufferptr buf, farptr msg, int size)
  133. {
  134.    int res;
  135.    CRITICAL;
  136.  
  137.    res = -1;
  138.    C_ENTER;
  139.  
  140.    if (buf->pip.filled && buf->buf_read.state)
  141.       {
  142.       request_resource (&buf->buf_read, 0L);
  143.       C_LEAVE;
  144.       res = read_buffer (buf, msg, size, 0L);
  145.       }
  146.    else
  147.       C_LEAVE;
  148.  
  149.    return res;
  150. }
  151.  
  152.  
  153. local int near tsk_wrbuf (bufferptr buf, word w, dword timeout)
  154. {
  155.    int i;
  156.  
  157.    if ((i = write_wpipe (&buf->pip, w, timeout)) < 0)
  158.       release_resource (&buf->buf_write);
  159.    return i;
  160. }
  161.  
  162.  
  163. int far write_buffer (bufferptr buf, farptr msg, int size, dword timeout)
  164. {
  165.    int i, res, l2;
  166.  
  167.    if (size < 0 || (word)((size + 3) >> 1) > buf->pip.bufsize)
  168.       return -3;
  169.  
  170.    if ((i = request_resource (&buf->buf_write, timeout)) < 0)
  171.       return i;
  172.  
  173.    if ((i = tsk_wrbuf (buf, (word)size, timeout)) < 0)
  174.       return i;
  175.  
  176.    l2 = (size + 1) >> 1;
  177.  
  178.    for (i = 0; i < l2; i++)
  179.       if ((res = tsk_wrbuf (buf, ((wordptr)msg) [i], timeout)) < 0)
  180.          return res;
  181.  
  182.    buf->msgcnt++;
  183.  
  184.    release_resource (&buf->buf_write);
  185.    return size;
  186. }
  187.  
  188.  
  189. int far c_write_buffer (bufferptr buf, farptr msg, int size)
  190. {
  191.    int res;
  192.    CRITICAL;
  193.  
  194.    if (size < 0 || (word)((size + 3) >> 1) > buf->pip.bufsize)
  195.       return -3;
  196.  
  197.    C_ENTER;
  198.    res = -1;
  199.  
  200.    if (wpipe_free (&buf->pip) >= ((size + 3) >> 1) && buf->buf_write.state)
  201.       {
  202.       request_resource (&buf->buf_write, 0L);
  203.       C_LEAVE;
  204.       res = write_buffer (buf, msg, size, 0L);
  205.       }
  206.    else
  207.       C_LEAVE;
  208.  
  209.    return res;
  210. }
  211.  
  212.  
  213. word far check_buffer (bufferptr buf)
  214. {
  215.    return buf->msgcnt;
  216. }
  217.  
  218.